package controller

import (
	"github.com/gin-gonic/gin"
	"net"
	"net/http"
)

func AddressesController(c *gin.Context) {
	addrs, _ := net.InterfaceAddrs() //获取电脑所有的ip地址
	var result []string
	for _, address := range addrs {
		// check the address type and if it is not a loopback the display it
		//由于address可能的类型太多了，是一个接口
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			//address.(*net.IPNet)是一个类型断言
			if ipnet.IP.To4() != nil {
				// To4 converts the IPv4 address ip to a 4-byte representation.
				result = append(result, ipnet.IP.String())
			}
		}
	}
	c.JSON(http.StatusOK, gin.H{"addresses": result})
	//总体就是遍历所有的地址，然后把所有ip地址放到result里面
	//然后把result作为一个json返回给前端
}
